package com.lute.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lute.model.Admin;
import com.lute.model.Approver;
import com.lute.model.BusinessUnit;
import com.lute.model.Client;
import com.lute.model.ClientAccountingPeriod;
import com.lute.model.ClientAccountingPeriodState;
import com.lute.model.ClientAdmin;
import com.lute.model.ClientAdminProfile;
import com.lute.model.ClientCategory;
import com.lute.model.ClientHoursCategory;
import com.lute.model.ClientProfile;
import com.lute.model.CompanyCategory;
import com.lute.model.CompanyHoursCategory;
import com.lute.model.Employee;
import com.lute.model.EmployeeAccountingPeriod;
import com.lute.model.EmployeeAccountingPeriodState;
import com.lute.model.EmployeeCurrentState;
import com.lute.model.EmployeeEntry;
import com.lute.model.EmployeeProfile;
import com.lute.model.EmploymentStatusCategories;
import com.lute.model.NotepadEntry;
import com.lute.model.Position;
import com.lute.model.PositionName;
import com.lute.model.Role;
import com.lute.model.User;
import com.lute.utils.ServerErrorResponse;

@Controller
public class LoadTestDataServlet {
	
	@RequestMapping (value="/populateDatabase",method= RequestMethod.GET)
	public @ResponseBody String resetDatabase(HttpServletRequest request) throws JSONException {
		String result = "";
		JSONObject jsonReq;
		JSONObject jsonRes = new JSONObject();
		
		HttpSession session = request.getSession(false);
		if(session == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_LOGGED_IN.toString());
			result = jsonRes.toString();
			return result;
		}
		String role = (String)session.getAttribute("role");
		// value for testing
		//String role = "admin";
		
		
		/* check  authorization */
//		if(!(role.equals("admin"))) {
//			jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
//			result = jsonRes.toString();
//			System.out.println(result);
//			return result;
//		}
		
		AnnotationConfiguration config = new AnnotationConfiguration();
		config.addAnnotatedClass(Admin.class);
		config.addAnnotatedClass(Approver.class);
		config.addAnnotatedClass(BusinessUnit.class);
		config.addAnnotatedClass(Client.class);
		config.addAnnotatedClass(ClientAccountingPeriod.class);
		config.addAnnotatedClass(ClientAdmin.class);
		config.addAnnotatedClass(ClientAdminProfile.class);
		config.addAnnotatedClass(ClientCategory.class);
		config.addAnnotatedClass(ClientProfile.class);
		config.addAnnotatedClass(CompanyCategory.class);
		config.addAnnotatedClass(Employee.class);
		config.addAnnotatedClass(EmployeeAccountingPeriod.class);
		config.addAnnotatedClass(EmployeeAccountingPeriodState.class);
		config.addAnnotatedClass(EmployeeEntry.class);
		config.addAnnotatedClass(EmployeeProfile.class);
		config.addAnnotatedClass(NotepadEntry.class);
		config.addAnnotatedClass(Position.class);
		config.addAnnotatedClass(Role.class);
		config.addAnnotatedClass(User.class);
		
		config.configure("hibernate.cfg.xml");
		try {
			new SchemaExport(config).drop(true, true);
		}catch(Exception e) {
			jsonRes.put("result", ServerErrorResponse.AUTHENTICATION_SERVICE_DOWN.toString());
			result = jsonRes.toString();
			System.out.println(result);
			return result;
		}
		
		
		SessionFactory factory = config.buildSessionFactory();
		Session hibernateSession = factory.getCurrentSession();
		hibernateSession.beginTransaction();
		
		/*
		 * CLIENT CATEGORY + COMPANY CATEGORY
		 */
		ClientCategory client1CatBusinessTrip = new ClientCategory();
		client1CatBusinessTrip.setName(ClientHoursCategory.BUSINESSTRIP.toString());
		
		ClientCategory client1CatContractorTimeOff = new ClientCategory();
		client1CatContractorTimeOff.setName(ClientHoursCategory.CONTRACTORTIMEOFF.toString());
		
		ClientCategory client1CatNormalTime = new ClientCategory();
		client1CatNormalTime.setName(ClientHoursCategory.NORMALTIME.toString());
		
		ClientCategory client1CatOverTime = new ClientCategory();
		client1CatOverTime.setName(ClientHoursCategory.OVERTIME.toString());
		
		ClientCategory client1CatTraining = new ClientCategory();
		client1CatTraining.setName(ClientHoursCategory.TRAINING.toString());

		ClientCategory client2CatBusinessTrip = new ClientCategory();
		client2CatBusinessTrip.setName(ClientHoursCategory.BUSINESSTRIP.toString());
		
		ClientCategory client2CatContractorTimeOff = new ClientCategory();
		client2CatContractorTimeOff.setName(ClientHoursCategory.CONTRACTORTIMEOFF.toString());
		
		ClientCategory client2CatNormalTime = new ClientCategory();
		client2CatNormalTime.setName(ClientHoursCategory.NORMALTIME.toString());
		
		ClientCategory client2CatOverTime = new ClientCategory();
		client2CatOverTime.setName(ClientHoursCategory.OVERTIME.toString());
		
		ClientCategory client2CatTraining = new ClientCategory();
		client2CatTraining.setName(ClientHoursCategory.TRAINING.toString());
		

		
		
		CompanyCategory companyCategory1 = new CompanyCategory();
		companyCategory1.setName(CompanyHoursCategory.NATIONAL_HOLIDAY.toString());
		
		CompanyCategory companyCategory2 = new CompanyCategory();
		companyCategory2.setName(CompanyHoursCategory.BUSINESS_TRIP_CLIENT.toString());
		
		CompanyCategory companyCategory3 = new CompanyCategory();
		companyCategory3.setName(CompanyHoursCategory.BUSINESS_TRIP_COMPANY.toString());
		
		CompanyCategory companyCategory4 = new CompanyCategory();
		companyCategory4.setName(CompanyHoursCategory.MATERNITY_LEAVE.toString());
		
		CompanyCategory companyCategory5 = new CompanyCategory();
		companyCategory5.setName(CompanyHoursCategory.OVERTIME_CLIENT.toString());
		
		CompanyCategory companyCategory6 = new CompanyCategory();
		companyCategory6.setName(CompanyHoursCategory.OVERTIME_COMPANY.toString());
		
		CompanyCategory companyCategory7 = new CompanyCategory();
		companyCategory7.setName(CompanyHoursCategory.PATERNITY_LEAVE.toString());
		
		CompanyCategory companyCategory8 = new CompanyCategory();
		companyCategory8.setName(CompanyHoursCategory.SICK_LEAVE.toString());
		
		CompanyCategory companyCategory9 = new CompanyCategory();
		companyCategory9.setName(CompanyHoursCategory.TRAINING_CLIENT.toString());
		
		CompanyCategory companyCategory10 = new CompanyCategory();
		companyCategory10.setName(CompanyHoursCategory.TRAINING_COMPANY.toString());
		
		CompanyCategory companyCategory11 = new CompanyCategory();
		companyCategory11.setName(CompanyHoursCategory.UNDEFINED.toString());
		
		CompanyCategory companyCategory12 = new CompanyCategory();
		companyCategory12.setName(CompanyHoursCategory.VACATION.toString());
		
		CompanyCategory companyCategory13 = new CompanyCategory();
		companyCategory13.setName(CompanyHoursCategory.WORK_CLIENT.toString());
		
		CompanyCategory companyCategory14 = new CompanyCategory();
		companyCategory14.setName(CompanyHoursCategory.WORK_COMPANY.toString());
		
		companyCategory1.getClientCategories().add(client1CatContractorTimeOff);
		companyCategory2.getClientCategories().add(client1CatBusinessTrip);
		companyCategory3.getClientCategories().add(client1CatContractorTimeOff);
		companyCategory4.getClientCategories().add(client1CatContractorTimeOff);
		companyCategory5.getClientCategories().add(client1CatOverTime);
		companyCategory6.getClientCategories().add(client1CatContractorTimeOff);
		companyCategory7.getClientCategories().add(client1CatContractorTimeOff);
		companyCategory8.getClientCategories().add(client1CatContractorTimeOff);
		companyCategory9.getClientCategories().add(client1CatTraining);
		companyCategory10.getClientCategories().add(client1CatContractorTimeOff);
		companyCategory11.getClientCategories().add(client1CatContractorTimeOff);
		companyCategory12.getClientCategories().add(client1CatContractorTimeOff);
		companyCategory13.getClientCategories().add(client1CatNormalTime);
		companyCategory14.getClientCategories().add(client1CatContractorTimeOff);

		companyCategory1.getClientCategories().add(client2CatContractorTimeOff);
		companyCategory2.getClientCategories().add(client2CatBusinessTrip);
		companyCategory3.getClientCategories().add(client2CatContractorTimeOff);
		companyCategory4.getClientCategories().add(client2CatContractorTimeOff);
		companyCategory5.getClientCategories().add(client2CatOverTime);
		companyCategory6.getClientCategories().add(client2CatContractorTimeOff);
		companyCategory7.getClientCategories().add(client2CatContractorTimeOff);
		companyCategory8.getClientCategories().add(client2CatContractorTimeOff);
		companyCategory9.getClientCategories().add(client2CatTraining);
		companyCategory10.getClientCategories().add(client2CatContractorTimeOff);
		companyCategory11.getClientCategories().add(client2CatContractorTimeOff);
		companyCategory12.getClientCategories().add(client2CatContractorTimeOff);
		companyCategory13.getClientCategories().add(client2CatNormalTime);
		companyCategory14.getClientCategories().add(client2CatContractorTimeOff);
		
		client1CatBusinessTrip.getCompanyCategories().add(companyCategory2);
		client1CatNormalTime.getCompanyCategories().add(companyCategory13);
		client1CatOverTime.getCompanyCategories().add(companyCategory5);
		client1CatTraining.getCompanyCategories().add(companyCategory9);
		
		client1CatContractorTimeOff.getCompanyCategories().add(companyCategory1);
		client1CatContractorTimeOff.getCompanyCategories().add(companyCategory3);
		client1CatContractorTimeOff.getCompanyCategories().add(companyCategory4);
		client1CatContractorTimeOff.getCompanyCategories().add(companyCategory6);
		client1CatContractorTimeOff.getCompanyCategories().add(companyCategory7);
		client1CatContractorTimeOff.getCompanyCategories().add(companyCategory8);
		client1CatContractorTimeOff.getCompanyCategories().add(companyCategory10);
		client1CatContractorTimeOff.getCompanyCategories().add(companyCategory11);
		client1CatContractorTimeOff.getCompanyCategories().add(companyCategory12);
		client1CatContractorTimeOff.getCompanyCategories().add(companyCategory14);

		client2CatBusinessTrip.getCompanyCategories().add(companyCategory2);
		client2CatNormalTime.getCompanyCategories().add(companyCategory13);
		client2CatOverTime.getCompanyCategories().add(companyCategory5);
		client2CatTraining.getCompanyCategories().add(companyCategory9);
		
		client2CatContractorTimeOff.getCompanyCategories().add(companyCategory1);
		client2CatContractorTimeOff.getCompanyCategories().add(companyCategory3);
		client2CatContractorTimeOff.getCompanyCategories().add(companyCategory4);
		client2CatContractorTimeOff.getCompanyCategories().add(companyCategory6);
		client2CatContractorTimeOff.getCompanyCategories().add(companyCategory7);
		client2CatContractorTimeOff.getCompanyCategories().add(companyCategory8);
		client2CatContractorTimeOff.getCompanyCategories().add(companyCategory10);
		client2CatContractorTimeOff.getCompanyCategories().add(companyCategory11);
		client2CatContractorTimeOff.getCompanyCategories().add(companyCategory12);
		client2CatContractorTimeOff.getCompanyCategories().add(companyCategory14);
		
		hibernateSession.save(client1CatBusinessTrip);
		hibernateSession.save(client1CatContractorTimeOff);
		hibernateSession.save(client1CatNormalTime);
		hibernateSession.save(client1CatOverTime);
		hibernateSession.save(client1CatTraining);
		hibernateSession.save(client2CatBusinessTrip);
		hibernateSession.save(client2CatContractorTimeOff);
		hibernateSession.save(client2CatNormalTime);
		hibernateSession.save(client2CatOverTime);
		hibernateSession.save(client2CatTraining);
		hibernateSession.save(companyCategory1);
		hibernateSession.save(companyCategory2);
		hibernateSession.save(companyCategory3);
		hibernateSession.save(companyCategory4);
		hibernateSession.save(companyCategory5);
		hibernateSession.save(companyCategory6);
		hibernateSession.save(companyCategory7);
		hibernateSession.save(companyCategory8);
		hibernateSession.save(companyCategory9);
		hibernateSession.save(companyCategory10);
		hibernateSession.save(companyCategory11);
		hibernateSession.save(companyCategory12);
		hibernateSession.save(companyCategory13);
		hibernateSession.save(companyCategory14);
		/*
		 * END OF -------------- CLIENT CATEGORY + COMPANY CATEGORY
		 */

		
		/*
		 * ROLE
		 */
		Role roleEmployeeTest = new Role();
		roleEmployeeTest.setName("employeeTest");
		hibernateSession.save(roleEmployeeTest);

		Role roleApprover = new Role();
		roleApprover.setName("approver");
		hibernateSession.save(roleApprover);

		Role roleClientAdmin = new Role();
		roleClientAdmin.setName("clientAdmin");
		hibernateSession.save(roleClientAdmin);
			
		Role roleAdmin = new Role();
		roleAdmin.setName("admin");
		hibernateSession.save(roleAdmin);
			
		Role roleEmployee = new Role();
		roleEmployee.setName("employee");
		hibernateSession.save(roleEmployee);
		/*
		 * END OF -------------- ROLE
		 */

		
		/*
		 * POSITION
		 */
		Position position1 = new Position();
		position1.setName(PositionName.BUSINESS_ANALYST.toString());
		position1.setCreativity_percentage(PositionName.BUSINESS_ANALYST.getPercentage());
		hibernateSession.save(position1);
		
		Position position2 = new Position();
		position2.setName(PositionName.DEVELOPER.toString());
		position2.setCreativity_percentage(PositionName.DEVELOPER.getPercentage());
		hibernateSession.save(position2);

		Position position3 = new Position();
		position3.setName(PositionName.QA.toString());
		position3.setCreativity_percentage(PositionName.QA.getPercentage());
		hibernateSession.save(position3);
		
		Position position4 = new Position();
		position4.setName(PositionName.SUPPORT.toString());
		position4.setCreativity_percentage(PositionName.SUPPORT.getPercentage());
		hibernateSession.save(position4);
		/*
		 * END OF -------------- POSITION
		 */

		
		/*
		 * CLIENT
		 */
		Client client1 = new Client();
		client1.setName("Client1");
		hibernateSession.save(client1);
			
		Client client2 = new Client();
		client2.setName("Client2");
		hibernateSession.save(client2);
		/*
		 * END OF -------------- CLIENT
		 */
		
		
		/*
		 * BUSINESS UNIT
		 */
		BusinessUnit businessUnit1 = new BusinessUnit();
		businessUnit1.setName("Client1 BU1");
		businessUnit1.setClient(client1);
		client1.getBusiness_units().add(businessUnit1);
		hibernateSession.save(businessUnit1);
		
		BusinessUnit businessUnit2 = new BusinessUnit();
		businessUnit2.setName("Client1 BU2");
		businessUnit2.setClient(client1);
		client1.getBusiness_units().add(businessUnit2);
		hibernateSession.save(businessUnit2);
		
		BusinessUnit businessUnit3 = new BusinessUnit();
		businessUnit3.setName("Client2 BU1");
		businessUnit3.setClient(client2);
		client2.getBusiness_units().add(businessUnit3);
		hibernateSession.save(businessUnit3);
		/*
		 * END OF -------------- BUSINESS UNIT
		 */

		
		/*
		 * ADMIN
		 */
		Admin admin = new Admin();
		admin.setFirst_name("Admin");
		admin.setLast_name("Admin");
		admin.setEmail("luteee@adminnnlute.com");
		admin.setLogin("admin");
		admin.setRole(roleAdmin);
		admin.setAccountLocked("active");
		hibernateSession.save(admin);
		
		/*
		 * END OF --------------------- ADMIN
		 */
		
		
		/*
		 * CLIENT ADMIN 1st + CLIENT ADMIN PROFILE
		 */
		ClientAdmin clientAdmin1 =  new ClientAdmin();
		clientAdmin1.setLogin("luteclientadmin");
		clientAdmin1.setRole(roleClientAdmin);
		clientAdmin1.setEmail("luteca2013@gmail.com");
		clientAdmin1.setFirst_name("luteclientadmin");
		clientAdmin1.setLast_name("luteclientadmin");
		clientAdmin1.setAccountLocked("active");

//		ClientAdmin clientAdmin2 = new ClientAdmin();
//		clientAdmin2.setLogin("luteclientadmin2");
//		clientAdmin2.setRole(roleClientAdmin);
//		clientAdmin2.setEmail("luteca2013@gmail.com");
//		clientAdmin2.setFirst_name("luteclientadmin2");
//		clientAdmin2.setLast_name("luteclientadmin2");
//		clientAdmin2.setAccountLocked("active");

		ClientAdminProfile clientAdminProfile1 = new ClientAdminProfile();
		clientAdminProfile1.setEmail(clientAdmin1.getEmail());
		clientAdminProfile1.setClientAdmin(clientAdmin1);
		clientAdminProfile1.setDefaultClientId((Integer)client1.getId_client());
		clientAdminProfile1.getClients().add(client1);
		clientAdminProfile1.getClients().add(client2);
		
		hibernateSession.save(clientAdmin1);
		hibernateSession.save(clientAdminProfile1);
		/*
		 * END OF -------------- CLIENT ADMIN + CLIENT ADMIN PROFILE
		 */

		
	/*
	 * EMPLOYEE 1st
	 */
		
		/*
		 * APPROVER1 TEAM 
		 */
		Employee employee1 = new Employee();
		employee1.setEmail("bartosz.szewczyk.81@gmail.com");
		employee1.setFirst_name("Bartosz");
		employee1.setLast_name("Szewczyk");
		employee1.setLogin("bszewczyk");
		employee1.setRole(roleEmployee);
		employee1.setAccountLocked("active");
		hibernateSession.save(employee1);
		
		
		Employee employee2 = new Employee();
		employee2.setEmail("luteuser1@gmail.com");
		employee2.setFirst_name("luteuser1");
		employee2.setLast_name("luteuser1");
		employee2.setLogin("luteuser1");
		employee2.setRole(roleEmployee);
		employee2.setAccountLocked("active");
		hibernateSession.save(employee2);

		
		Employee employee3 = new Employee();
		employee3.setEmail("luteuser2@gmail.com");
		employee3.setFirst_name("luteuser2");
		employee3.setLast_name("luteuser2");
		employee3.setLogin("luteuser2");
		employee3.setRole(roleEmployee);
		employee3.setAccountLocked("active");
		hibernateSession.save(employee3);

		
		Employee employee4 = new Employee();
		employee4.setEmail("luteuser3@gmail.com");
		employee4.setFirst_name("luteuser3");
		employee4.setLast_name("luteuser3");
		employee4.setLogin("luteuser3");
		employee4.setRole(roleEmployee);
		employee4.setAccountLocked("active");
		hibernateSession.save(employee4);
		/*
		 * END OF -------------- APPROVER1 TEAM
		 */
			
		
		
		/*
		 * APPROVER2 TEAM 
		 */

		Employee employee11 = new Employee();
		employee11.setEmail("luteuser1@gmail.com");
		employee11.setFirst_name("luteuser11");
		employee11.setLast_name("luteuser11");
		employee11.setLogin("luteuser11");
		employee11.setRole(roleEmployee);
		employee11.setAccountLocked("active");
		hibernateSession.save(employee11);

		Employee employee12 = new Employee();
		employee12.setEmail("luteuser2@gmail.com");
		employee12.setFirst_name("luteuser12");
		employee12.setLast_name("luteuser12");
		employee12.setLogin("luteuser12");
		employee12.setRole(roleEmployee);
		employee12.setAccountLocked("active");
		hibernateSession.save(employee12);

		/*
		 * END OF -------------- APPROVER2 TEAM
		 */

			
		/*
		 * APPROVER
		 */
		Approver approver1 = new Approver();
		approver1.setEmail("luteapprover@gmail.com");
		approver1.setFirst_name("luteapprover");
		approver1.setLast_name("luteapprover");
		approver1.setLogin("luteapprover");
		approver1.setRole(roleApprover);
			
		Approver approver2 = new Approver();
		approver2.setEmail("luteapprover@gmail.com");
		approver2.setFirst_name("luteapprover2");
		approver2.setLast_name("luteapprover2");
		approver2.setLogin("luteapprover2");
		approver2.setRole(roleApprover);
		
		approver1.setReplacement(null);
		approver2.setReplacement(null);
		
		hibernateSession.save(approver1);
		hibernateSession.save(approver2);
		/*
		 * END OF -------------- APPROVER
		 */
		
		//SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		GregorianCalendar cal = new GregorianCalendar();
		dateFormater.setCalendar(cal);

		/*
		 * END OF -------------- EMPLOYEE ACCOUNTING PERIOD + EMPLOYEE
		 */
		EmployeeProfile employeeProfile1 = new EmployeeProfile();
		employeeProfile1.setEmployee(employee1);
		employeeProfile1.setApprover(null);
		employeeProfile1.setClient(client1);
		employeeProfile1.setEmployeeApprover((Integer)approver1.getId_user());
		employeeProfile1.setBusinessUnit(businessUnit1);
		employeeProfile1.setEmail(employee1.getEmail());
		employeeProfile1.setPositions(position1);
		employeeProfile1.setEmploymentStatus(EmploymentStatusCategories.EMPLOYMENT_CONTRACT.toString());
		employeeProfile1.setBillingStartDate(new GregorianCalendar(2013,8,01));
		employeeProfile1.setBackgroundCheck(new GregorianCalendar(2013,07,31));
		employeeProfile1.setEmployeeCurrentState(EmployeeCurrentState.WORKING_FOR_CLIENT.toString());
		employeeProfile1.setStartWorking(new GregorianCalendar(2013,06,01));
		employeeProfile1.setWorkLifeLine(new GregorianCalendar(2013,06,01));
		employee1.setEmployeeProfil(employeeProfile1);

		
		EmployeeProfile employeeProfile2 = new EmployeeProfile();
		employeeProfile2.setEmployee(employee2);
		employeeProfile2.setApprover(null);
		employeeProfile2.setClient(client1);
		employeeProfile2.setEmployeeApprover((Integer)approver1.getId_user());
		employeeProfile2.setBusinessUnit(businessUnit1);
		employeeProfile2.setEmail(employee2.getEmail());
		employeeProfile2.setPositions(position2);
		employeeProfile2.setEmploymentStatus(EmploymentStatusCategories.EMPLOYMENT_CONTRACT.toString());
		employeeProfile2.setBillingStartDate(new GregorianCalendar(2013,01,01));
		employeeProfile2.setBackgroundCheck(new GregorianCalendar(2013,03,31));
		employeeProfile2.setEmployeeCurrentState(EmployeeCurrentState.WORKING_FOR_CLIENT.toString());
		employeeProfile2.setStartWorking(new GregorianCalendar(2012,12,01));
		employeeProfile2.setWorkLifeLine(new GregorianCalendar(2013,12,01));
		employee2.setEmployeeProfil(employeeProfile2);
		
		EmployeeProfile employeeProfile3 = new EmployeeProfile();
		employeeProfile3.setEmployee(employee3);
		employeeProfile3.setApprover(null);
		employeeProfile3.setClient(client1);
		employeeProfile3.setEmployeeApprover((Integer)approver1.getId_user());
		employeeProfile3.setBusinessUnit(businessUnit1);
		employeeProfile3.setEmail(employee3.getEmail());
		employeeProfile3.setPositions(position4);
		employeeProfile3.setEmploymentStatus(EmploymentStatusCategories.EMPLOYMENT_CONTRACT.toString());
		employeeProfile3.setBillingStartDate(new GregorianCalendar(2013,02,01));
		employeeProfile3.setBackgroundCheck(new GregorianCalendar(2013,02,24));
		employeeProfile3.setEmployeeCurrentState(EmployeeCurrentState.BENCHED.toString());
		employeeProfile3.setStartWorking(new GregorianCalendar(2012,01,01));
		employeeProfile3.setWorkLifeLine(new GregorianCalendar(2013,01,01));
		employee3.setEmployeeProfil(employeeProfile3);
		
		EmployeeProfile employeeProfile4 = new EmployeeProfile();
		employeeProfile4.setEmployee(employee4);
		employeeProfile4.setApprover(null);
		employeeProfile4.setClient(client1);
		employeeProfile4.setEmployeeApprover((Integer)approver1.getId_user());
		employeeProfile4.setBusinessUnit(businessUnit1);
		employeeProfile4.setEmail(employee4.getEmail());
		employeeProfile4.setPositions(position3);
		employeeProfile4.setEmploymentStatus(EmploymentStatusCategories.EMPLOYMENT_CONTRACT.toString());
		employeeProfile4.setBillingStartDate(new GregorianCalendar(2013,05,10));
		employeeProfile4.setBackgroundCheck(new GregorianCalendar(2013,06,24));
		employeeProfile4.setEmployeeCurrentState(EmployeeCurrentState.WORKING_FOR_CLIENT.toString());
		employeeProfile4.setStartWorking(new GregorianCalendar(2012,04,01));
		employeeProfile4.setWorkLifeLine(new GregorianCalendar(2013,04,01));
		employee4.setEmployeeProfil(employeeProfile4);

		
		EmployeeProfile employeeProfile11 = new EmployeeProfile();
		employeeProfile11.setEmployee(employee11);
		employeeProfile11.setApprover(null);
		employeeProfile11.setClient(client2);
		employeeProfile11.setEmployeeApprover((Integer)approver2.getId_user());
		employeeProfile11.setBusinessUnit(businessUnit3);
		employeeProfile11.setEmail(employee11.getEmail());
		employeeProfile11.setPositions(position2);
		employeeProfile11.setEmploymentStatus(EmploymentStatusCategories.EMPLOYMENT_CONTRACT.toString());
		employeeProfile11.setBillingStartDate(new GregorianCalendar(2013,05,10));
		employeeProfile11.setBackgroundCheck(new GregorianCalendar(2013,06,24));
		employeeProfile11.setEmployeeCurrentState(EmployeeCurrentState.WORKING_FOR_CLIENT.toString());
		employeeProfile11.setStartWorking(new GregorianCalendar(2012,04,01));
		employeeProfile11.setWorkLifeLine(new GregorianCalendar(2013,04,01));
		employee11.setEmployeeProfil(employeeProfile11);

		EmployeeProfile employeeProfile12 = new EmployeeProfile();
		employeeProfile12.setEmployee(employee12);
		employeeProfile12.setApprover(null);
		employeeProfile12.setClient(client2);
		employeeProfile12.setEmployeeApprover((Integer)approver2.getId_user());
		employeeProfile12.setBusinessUnit(businessUnit3);
		employeeProfile12.setEmail(employee12.getEmail());
		employeeProfile12.setPositions(position3);
		employeeProfile12.setEmploymentStatus(EmploymentStatusCategories.EMPLOYMENT_CONTRACT.toString());
		employeeProfile12.setBillingStartDate(new GregorianCalendar(2013,05,10));
		employeeProfile12.setBackgroundCheck(new GregorianCalendar(2013,06,24));
		employeeProfile12.setEmployeeCurrentState(EmployeeCurrentState.WORKING_FOR_CLIENT.toString());
		employeeProfile12.setStartWorking(new GregorianCalendar(2012,04,01));
		employeeProfile12.setWorkLifeLine(new GregorianCalendar(2013,04,01));
		employee12.setEmployeeProfil(employeeProfile12);

		// approver profiles
		EmployeeProfile approverProfile1 = new EmployeeProfile();
		approverProfile1.setEmployee(null);
		approverProfile1.setApprover(approver1);
		approverProfile1.setClient(client1);
		approverProfile1.setEmployeeApprover(null);
		approverProfile1.setBusinessUnit(businessUnit1);
		approverProfile1.setEmail(approver1.getEmail());
		approverProfile1.setPositions(position1);
		approverProfile1.setEmploymentStatus(EmploymentStatusCategories.EMPLOYMENT_CONTRACT.toString());
		approverProfile1.setBillingStartDate(new GregorianCalendar(2012,05,10));
		approverProfile1.setBackgroundCheck(new GregorianCalendar(2012,07,24));
		approverProfile1.setEmployeeCurrentState(EmployeeCurrentState.WORKING_FOR_CLIENT.toString());
		approverProfile1.setStartWorking(new GregorianCalendar(2012,04,01));
		approverProfile1.setWorkLifeLine(new GregorianCalendar(2012,05,21));
		approver1.setApproverProfile(approverProfile1);

		EmployeeProfile approverProfile2 = new EmployeeProfile();
		approverProfile2.setEmployee(null);
		approverProfile2.setApprover(approver2);
		approverProfile2.setClient(client2);
		approverProfile2.setEmployeeApprover(null);
		approverProfile2.setBusinessUnit(businessUnit3);
		approverProfile2.setEmail(approver2.getEmail());
		approverProfile2.setPositions(position3);
		approverProfile2.setEmploymentStatus(EmploymentStatusCategories.EMPLOYMENT_CONTRACT.toString());
		approverProfile2.setBillingStartDate(new GregorianCalendar(2012,05,10));
		approverProfile2.setBackgroundCheck(new GregorianCalendar(2012,07,24));
		approverProfile2.setEmployeeCurrentState(EmployeeCurrentState.WORKING_FOR_CLIENT.toString());
		approverProfile2.setStartWorking(new GregorianCalendar(2012,04,01));
		approverProfile2.setWorkLifeLine(new GregorianCalendar(2012,05,21));
		approver2.setApproverProfile(approverProfile2);


		hibernateSession.save(employeeProfile1);
		hibernateSession.save(employeeProfile2);
		hibernateSession.save(employeeProfile3);
		hibernateSession.save(employeeProfile4);
		hibernateSession.save(employeeProfile11);
		hibernateSession.save(employeeProfile12);
		hibernateSession.save(employee1);
		hibernateSession.save(employee2);
		hibernateSession.save(employee3);
		hibernateSession.save(employee4);
		hibernateSession.save(employee11);
		hibernateSession.save(employee12);
		hibernateSession.save(approver1);
		hibernateSession.save(approver2);
		/*
		 * END OF -------------- EMPLOYEE ACCOUNTING PERIOD + EMPLOYEE
		 */

		
		
		/*
		 * APPROVER
		 */
		approver1.getEmployees().add(employee1);
		approver1.getEmployees().add(employee2);
		approver1.getEmployees().add(employee3);
		approver1.getEmployees().add(employee4);
		
		approver2.getEmployees().add(employee11);
		approver2.getEmployees().add(employee12);

		hibernateSession.save(approver1);
		hibernateSession.save(approver2);
		/*
		 * END OF -------------- APPROVER
		 */
		
		/*
		 * CLIENT + CLIENT PROFILE
		 */
		
		ClientProfile clientProfile1 = new ClientProfile();
		clientProfile1.setClient(client1);
		clientProfile1.getClientAdmin().add(clientAdmin1);

		ClientProfile clientProfile2 = new ClientProfile();
		clientProfile2.setClient(client2);
		clientProfile2.getClientAdmin().add(clientAdmin1);
		

		clientProfile1.getClientCategories().add(client1CatBusinessTrip);
		clientProfile1.getClientCategories().add(client1CatContractorTimeOff);
		clientProfile1.getClientCategories().add(client1CatNormalTime);
		clientProfile1.getClientCategories().add(client1CatOverTime);
		clientProfile1.getClientCategories().add(client1CatTraining);
		clientProfile1.setSendMailRemainder(new GregorianCalendar(2013,8,25));
		clientProfile1.setDeadlineToCloseAccountingPeriod(new GregorianCalendar(2013,8,31));
		client1.setClientProfil(clientProfile1);

		client1CatBusinessTrip.setClientProf(clientProfile1);
		client1CatContractorTimeOff.setClientProf(clientProfile1);
		client1CatNormalTime.setClientProf(clientProfile1);
		client1CatOverTime.setClientProf(clientProfile1);
		client1CatTraining.setClientProf(clientProfile1);

		clientProfile2.getClientCategories().add(client1CatBusinessTrip);
		clientProfile2.getClientCategories().add(client1CatContractorTimeOff);
		clientProfile2.getClientCategories().add(client1CatNormalTime);
		clientProfile2.getClientCategories().add(client1CatOverTime);
		clientProfile2.getClientCategories().add(client1CatTraining);
		clientProfile2.setSendMailRemainder(new GregorianCalendar(2013,8,25));
		clientProfile2.setDeadlineToCloseAccountingPeriod(new GregorianCalendar(2013,8,31));
		client2.setClientProfil(clientProfile2);
		
		client2CatBusinessTrip.setClientProf(clientProfile2);
		client2CatContractorTimeOff.setClientProf(clientProfile2);
		client2CatNormalTime.setClientProf(clientProfile2);
		client2CatOverTime.setClientProf(clientProfile2);
		client2CatTraining.setClientProf(clientProfile2);

		hibernateSession.save(clientProfile1);
		hibernateSession.save(clientProfile2);
		
		hibernateSession.save(client1CatBusinessTrip);
		hibernateSession.save(client1CatContractorTimeOff);
		hibernateSession.save(client1CatNormalTime);
		hibernateSession.save(client1CatOverTime);
		hibernateSession.save(client1CatTraining);
		hibernateSession.save(client2CatBusinessTrip);
		hibernateSession.save(client2CatContractorTimeOff);
		hibernateSession.save(client2CatNormalTime);
		hibernateSession.save(client2CatOverTime);
		hibernateSession.save(client2CatTraining);
		hibernateSession.save(client1);
		hibernateSession.save(client2);
		/*
		 * END OF -------------- CLIENT ADMIN
		 */
		EmployeeAccountingPeriod eAccPeriod1 = new EmployeeAccountingPeriod();
		eAccPeriod1.setApprover(null);
		eAccPeriod1.setEmployee(employee1);
		eAccPeriod1.setCalendarApprover(approver1.getId_user());
		eAccPeriod1.setRejectedBy(approver1.getFirst_name() + " " + approver1.getLast_name());
		eAccPeriod1.setRejectedByUserRole(approver1.getRole().getName());
		eAccPeriod1.setEmployeeAccountingPeriodState(EmployeeAccountingPeriodState.REJECTED_BY_APPROVER.toString());
		eAccPeriod1.setRejectionDate(new GregorianCalendar(2013,8,15));
		eAccPeriod1.setRejectionReason("cos tam");
		eAccPeriod1.setCalFrom(new GregorianCalendar(2013,8,01));
		employee1.getEmployeeAccountingPeriods().add(eAccPeriod1);

		EmployeeAccountingPeriod eAccPeriod2 = new EmployeeAccountingPeriod();
		eAccPeriod2.setApprover(null);
		eAccPeriod2.setEmployee(employee2);
		eAccPeriod2.setCalendarApprover(approver1.getId_user());
		eAccPeriod2.setRejectedBy(null);
		eAccPeriod2.setRejectedByUserRole(null);
		eAccPeriod2.setEmployeeAccountingPeriodState(EmployeeAccountingPeriodState.INITIAL.toString());
		eAccPeriod2.setRejectionDate(null);
		eAccPeriod2.setRejectionReason(null);
		eAccPeriod2.setCalFrom(new GregorianCalendar(2013,8,01));
		employee2.getEmployeeAccountingPeriods().add(eAccPeriod2);

		EmployeeAccountingPeriod eAccPeriod3 = new EmployeeAccountingPeriod();
		eAccPeriod3.setApprover(null);
		eAccPeriod3.setEmployee(employee3);
		eAccPeriod3.setCalendarApprover(approver1.getId_user());
		eAccPeriod3.setRejectedBy(approver1.getFirst_name() + " " + approver1.getLast_name());
		eAccPeriod3.setRejectedByUserRole(approver1.getRole().getName());
		eAccPeriod3.setEmployeeAccountingPeriodState(EmployeeAccountingPeriodState.REJECTED_BY_APPROVER.toString());
		eAccPeriod3.setRejectionDate(new GregorianCalendar(2013,8,15));
		eAccPeriod3.setRejectionReason("cos tam innego");
		eAccPeriod3.setCalFrom(new GregorianCalendar(2013,8,01));
		employee3.getEmployeeAccountingPeriods().add(eAccPeriod3);

		EmployeeAccountingPeriod eAccPeriod4 = new EmployeeAccountingPeriod();
		eAccPeriod4.setApprover(null);
		eAccPeriod4.setEmployee(employee4);
		eAccPeriod4.setCalendarApprover(approver1.getId_user());
		eAccPeriod4.setRejectedBy(null);
		eAccPeriod4.setRejectedByUserRole(null);
		eAccPeriod4.setEmployeeAccountingPeriodState(EmployeeAccountingPeriodState.SENT_TO_APPROVER.toString());
		eAccPeriod4.setRejectionDate(null);
		eAccPeriod4.setRejectionReason(null);
		eAccPeriod4.setCalFrom(new GregorianCalendar(2013,8,01));
		employee4.getEmployeeAccountingPeriods().add(eAccPeriod4);

		EmployeeAccountingPeriod eAccPeriod11 = new EmployeeAccountingPeriod();
		eAccPeriod11.setApprover(null);
		eAccPeriod11.setEmployee(employee11);
		eAccPeriod11.setCalendarApprover(approver2.getId_user());
		eAccPeriod11.setRejectedBy(null);
		eAccPeriod11.setRejectedByUserRole(null);
		eAccPeriod11.setEmployeeAccountingPeriodState(EmployeeAccountingPeriodState.SENT_TO_APPROVER.toString());
		eAccPeriod11.setRejectionDate(null);
		eAccPeriod11.setRejectionReason(null);
		eAccPeriod11.setCalFrom(new GregorianCalendar(2013,8,01));
		employee11.getEmployeeAccountingPeriods().add(eAccPeriod11);

		EmployeeAccountingPeriod eAccPeriod12 = new EmployeeAccountingPeriod();
		eAccPeriod12.setApprover(null);
		eAccPeriod12.setEmployee(employee12);
		eAccPeriod12.setCalendarApprover(approver2.getId_user());
		eAccPeriod12.setRejectedBy(null);
		eAccPeriod12.setRejectedByUserRole(null);
		eAccPeriod12.setEmployeeAccountingPeriodState(EmployeeAccountingPeriodState.SENT_TO_APPROVER.toString());
		eAccPeriod12.setRejectionDate(null);
		eAccPeriod12.setRejectionReason(null);
		eAccPeriod12.setCalFrom(new GregorianCalendar(2013,8,01));
		employee12.getEmployeeAccountingPeriods().add(eAccPeriod12);

		EmployeeAccountingPeriod approver1AccPeriod = new EmployeeAccountingPeriod();
		approver1AccPeriod.setApprover(approver1);
		approver1AccPeriod.setEmployee(null);
		approver1AccPeriod.setCalendarApprover(approver1.getId_user());
		approver1AccPeriod.setRejectedBy(null);
		approver1AccPeriod.setRejectedByUserRole(null);
		approver1AccPeriod.setEmployeeAccountingPeriodState(EmployeeAccountingPeriodState.CHECKED_BY_APPROVER.toString());
		approver1AccPeriod.setRejectionDate(null);
		approver1AccPeriod.setRejectionReason(null);
		approver1AccPeriod.setCalFrom(new GregorianCalendar(2013,8,01));
		approver1.getApproverAccountingPeriods().add(approver1AccPeriod);

		EmployeeAccountingPeriod approver2AccPeriod = new EmployeeAccountingPeriod();
		approver2AccPeriod.setApprover(approver2);
		approver2AccPeriod.setEmployee(null);
		approver2AccPeriod.setCalendarApprover(approver2.getId_user());
		approver2AccPeriod.setRejectedBy(null);
		approver2AccPeriod.setRejectedByUserRole(null);
		approver2AccPeriod.setEmployeeAccountingPeriodState(EmployeeAccountingPeriodState.CHECKED_BY_APPROVER.toString());
		approver2AccPeriod.setRejectionDate(null);
		approver2AccPeriod.setRejectionReason(null);
		approver2AccPeriod.setCalFrom(new GregorianCalendar(2013,8,01));
		approver2.getApproverAccountingPeriods().add(approver2AccPeriod);
		
		
		ClientAccountingPeriod cAccPeriod1 = new ClientAccountingPeriod();
		cAccPeriod1.setClient(client1);
		cAccPeriod1.setClientAccountingPeriodState(ClientAccountingPeriodState.OPEN.toString());
		cAccPeriod1.setDate_from(new GregorianCalendar(2013,8,01));
		cAccPeriod1.setDate_to(new GregorianCalendar(2013,8,30));
		cAccPeriod1.getEmployeeAccountingPeriod().add(eAccPeriod1);
		cAccPeriod1.getEmployeeAccountingPeriod().add(eAccPeriod2);
		cAccPeriod1.getEmployeeAccountingPeriod().add(eAccPeriod3);
		cAccPeriod1.getEmployeeAccountingPeriod().add(eAccPeriod4);
		cAccPeriod1.getEmployeeAccountingPeriod().add(approver1AccPeriod);
		client1.getClientAccountingPeriods().add(cAccPeriod1);

		ClientAccountingPeriod cAccPeriod2 = new ClientAccountingPeriod();
		cAccPeriod2.setClient(client2);
		cAccPeriod2.setClientAccountingPeriodState(ClientAccountingPeriodState.OPEN.toString());
		cAccPeriod2.setDate_from(new GregorianCalendar(2013,8,01));
		cAccPeriod2.setDate_to(new GregorianCalendar(2013,8,30));
		cAccPeriod2.getEmployeeAccountingPeriod().add(eAccPeriod11);
		cAccPeriod2.getEmployeeAccountingPeriod().add(eAccPeriod12);
		cAccPeriod2.getEmployeeAccountingPeriod().add(approver2AccPeriod);
		client2.getClientAccountingPeriods().add(cAccPeriod2);
		
		eAccPeriod1.setClientAccountingPeriod(cAccPeriod1);
		eAccPeriod2.setClientAccountingPeriod(cAccPeriod1);
		eAccPeriod3.setClientAccountingPeriod(cAccPeriod1);
		eAccPeriod4.setClientAccountingPeriod(cAccPeriod1);
		eAccPeriod11.setClientAccountingPeriod(cAccPeriod2);
		eAccPeriod12.setClientAccountingPeriod(cAccPeriod2);

		approver1AccPeriod.setClientAccountingPeriod(cAccPeriod1);
		approver2AccPeriod.setClientAccountingPeriod(cAccPeriod2);
		
		hibernateSession.save(cAccPeriod1);
		hibernateSession.save(eAccPeriod1);
		hibernateSession.save(eAccPeriod2);
		hibernateSession.save(eAccPeriod3);
		hibernateSession.save(eAccPeriod4);
		hibernateSession.save(eAccPeriod11);
		hibernateSession.save(eAccPeriod12);
		hibernateSession.save(approver1AccPeriod);
		hibernateSession.save(approver2AccPeriod);
		hibernateSession.save(cAccPeriod1);
		hibernateSession.save(cAccPeriod2);
		
		hibernateSession.getTransaction().commit();

		
		
		jsonRes.put("result","OK");
		result = jsonRes.toString();
		System.out.println(result);
		return result;
	}
}
